import { request } from "./index";
import nprogress from "nprogress";
import "nprogress/nprogress.css";
import store from "@/store";

request.interceptors.request.use((config) => {
	nprogress.start();

	let userTempId = store.state.user.userTempId;
	config.headers.userTempId = userTempId;

	const token = store.state.user.token;
	config.headers.token = token;

	return config;
});

request.interceptors.response.use(
	(response) => {
		nprogress.done();
		if (response.data.code === 200) {
			return response.data.data;
		} else {
			return Promise.reject({ message: response.data.message });
		}
	},
	(error) => {
		nprogress.done();
		return Promise.reject(error);
	}
);

export default request;
